package com.shadow.cloud.test.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程管理器
 **/
public class DistThreadManager {
    /**
     * 核心线程数
     */
    private static final int CORE_POOL_SIZE = 5;
    /**
     * 线程池最大线程数
     */
    private static final int MAX_POOL_SIZE = 10;
    /**
     * 额外线程空状态生存时间
     */
    private static final int KEEP_ALIVE_TIME = 30 * 1000;
    /**
     * 阻塞队列。当核心线程都被占用，且阻塞队列已满的情况下，才会开启额外线程。
     */
    private static final BlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(CORE_POOL_SIZE);
    private static final ThreadFactory FACTORY = new ThreadFactory() {
        private final AtomicInteger integer = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "ThreadPool thread: " + integer.getAndIncrement());
        }
    };

    /**
     * 缓存线程池对象
     */
    public static ThreadPoolExecutor MY_CACHE_THREAD_POOL  = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            KEEP_ALIVE_TIME,
            TimeUnit.SECONDS,
            WORK_QUEUE,
            FACTORY
    );

    private DistThreadManager() {
    }
}
